import tools from '../dll/libs/common/tools.js'

const fullZero = (num) => {
  return num < 10 ? '0' + num : num
}

const max = arr => Math.max.apply(null, arr);
const min = arr => Math.min.apply(null, arr)

// 获取x轴时间数据
const getDate = (fmt, len) => {
  let d = new Date()
  let res = []
  let a;
  for (let i = 0; i < len; i++) {
    switch (fmt) {
      case 'yyyyMMdd':
        a = `${d.getFullYear()}${fullZero(d.getMonth() + 1)}${fullZero(d.getDate())}`;
        break;
      case 'MM.dd':
        a = `${fullZero(d.getMonth() + 1)}.${fullZero(d.getDate())}`;
        break;
    }
    res.unshift(a)
    d = new Date(d.getTime() - 1000 * 60 * 60 * 24)
  }
  return res
};

// 异常数据检测方法
const abnDataDetect = (arr, lastIndex) => {
  let max = max(arr);
  let min = min(arr);
  let diffVal = (max - lastIndex) > (lastIndex - min) ? (max - lastIndex) : (lastIndex - min);
  if (diffVal > lastIndex / 50) {
    return {
      haveOutliers: true,
      value: (Math.floor(diffVal / lastIndex * 100 / 5) + 1) * 5
    }
  } else {
    return {
      haveOutliers: false,
      value: null
    }
  }
};

// 分钟刷新
let refPerMinute = {
  init: function(handle) {
    handle();
    const entryTime = new Date();  // 进入页面时间
    if (!verifyTime(entryTime)) return;  // 验证 进入页面时间 hh:mm   不在开盘时间内 不执行
    let tmpTime = entryTime.getTime();
    this.timer = setInterval(() => {
      if (!verifyTime(new Date(tmpTime += 60000))) return;
      console.log(new Date(tmpTime));
      handle();
    }, 60000)
  },
  destory: function() {
    clearInterval(this.timer)
  }
};

// 验证时间
const verifyTime = (val) => {
  let tmp = `${fullZero(val.getHours())}:${fullZero(val.getMinutes())}`;
  return tmp >= '09:30' && tmp <= '11:30' || tmp >= '13:00' && tmp <= '15:00'
};

// option对象属性的深拷贝
const extend = (initalObj, finalObj) => {
  if (finalObj == {}) {
    return initalObj;
  } else {
    for (let i in initalObj) {
      if (finalObj[i] == undefined) {
        finalObj[i] = initalObj[i]
      } else if (typeof initalObj[i] == 'object') {
        extend(initalObj[i], finalObj[i])
      }
    }
    return finalObj;
  }
};
const getNowTime = () => {
  let date = new Date();
  let str = '';
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  month = month < 10 ? '0' + month : month;
  let day = date.getDate();
  day = day < 10 ? '0' + day : day;
  str += year += month += day;
  return str;
}

//  获取总览页大盘时间数据
const getTradeTimeArr = () => {
  let resultArr = [],
    dateTmp = new Date(2000, 1, 1, 9, 30);

  resultArr.push(tools.DateFormat(dateTmp, 'HH:mm'));
  for (let i = 0; i < 120; i++) {
    dateTmp.setMinutes(dateTmp.getMinutes() + 1);
    resultArr.push(tools.DateFormat(dateTmp, 'HH:mm') == '11:30' ? '11:30/13:00' : tools.DateFormat(dateTmp, 'HH:mm'));
  }

  dateTmp = new Date(2000, 1, 1, 13, 1);
  resultArr.push(tools.DateFormat(dateTmp, 'HH:mm'));
  for (let i = 0; i < 119; i++) {
    dateTmp.setMinutes(dateTmp.getMinutes() + 1);
    resultArr.push(tools.DateFormat(dateTmp, 'HH:mm'));
  }
  return resultArr;
};

const getIndexMaxChg = data => {
  const range = max([Math.abs(max(data.index)) - data.closing, Math.abs(min(data.index) - data.closing)]) * 1.2;
  const chgRng = (range / data.closing * 100).toFixed(2);

  return chgRng
}

/** 大数据格式化，显示为万／亿  start**/
const decimal = decimal => num => num.toFixed(decimal);

const decimal0 = num => parseInt(num);

const decimal1 = decimal(1);

const decimal2 = decimal(2);

// 解析长数据
const destruct = number => {
  if (isNaN(number)) {
    return {
      isNegative: false,
      num: '',
      unit: ''
    }
  }
  const isNegative = number < 0;
  const num = Math.abs(number);
  const l = parseInt(num).toString().length;

  return {
    isNegative,
    num: l < 5 ? decimal0(num)
      : l < 7 ? decimal2(num / 10000)
      : l < 8 ? decimal1(num / 10000)
      : l < 9 ? decimal0(num / 10000)
      : l < 11 ? decimal2(num / 100000000)
      : l < 12 ? decimal1(num / 100000000)
      : decimal0(num / 100000000),
    unit: l < 5 ? '' : l < 9 ? '万' : '亿'
  }
}
/** 大数据格式化，显示为万／亿  end**/

/** 转换1013接口数据 ****/
const timeFromMinuts = m => {
  let h = m / 60;
  h = h > 10 ? h : '0' + h;

  let min = m % 60;
  min = min > 10 ? min : '0' + min;
  return `${h}:${min}`
}
const transform1013 = data => {
  let netNumber = [],
    mIn = [],
    mOut = [],
    mNetNumber = [],
    xTime = [];

  data.TableInfo.forEach(item => {
    xTime.push(timeFromMinuts(item.Minute));
    netNumber.push(item.ljjlr);
    mIn.push(item.bmoney);
    mOut.push(-item.smoney);
    mNetNumber.push(item.jlr)
  });

  return {
    xTime: xTime,
    netNumber: netNumber,
    minute: {
      in: mIn,
      out: mOut,
      mNetNumber: mNetNumber
    }
  }
}
/** 转换1013接口数据 ****/
const getUrlParam = (name, h) => {
  var url = decodeURI(location.search); // 获取url中"?"符后的字串
  var theRequest = {};
  if (url.indexOf('?') !== -1) {
    var str = url.substr(1);
    var strs = str.split('&');
    for (var i = 0; i < strs.length; i++) {
      theRequest[strs[i].split('=')[0]] = unescape(strs[i].split('=')[1]);
    }
  }
  return theRequest[name];
}

export {
  max,
  min,
  getDate,
  fullZero,
  extend,
  getNowTime,
  getTradeTimeArr,
  verifyTime,
  abnDataDetect,
  refPerMinute,
  getIndexMaxChg,
  destruct,
  transform1013,
  getUrlParam
}
